const controller = require("../../controllers/Controller").controller
const controllers = require('../../controllers/controllerIndex').controller

// pages/Index/DesignPosters/DesignPosters.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    icon1: "https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20210513/842452706747482112.png",
    icon2: "https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20210513/842452919725850624.png",
    list: [],
    totalPrice: 0,
    all: false,
    canPay:true
  },

  //获取设计单张海报价格
  getList() {
    controller.getDesignPosters().then(res => {
      let data = res.data
      data.select = false
      data.num = 1
      let list = []
      list.push(data)
      this.setData({
        list: list
      })
    })
  },

  //选中所有
  selectAll() {
    let list = this.data.list
    if (this.data.all == false) {
      list.forEach(item => {
        item.select = true
      })
      this.setData({
        all: true
      })
    } else {
      list.forEach(item => {
        item.select = false
      })
      this.setData({
        all: false
      })
    }
    this.setData({
      list: list
    })
    this.totalPrice()
  },

  //判断是否为全选
  isSelectAll() {
    let list = this.data.list
    let selectNum = 0
    list.forEach(item => {
      if (item.select == true) {
        selectNum++
      }
    })
    if (selectNum < list.length) {
      this.setData({
        all: false
      })
    } else {
      this.setData({
        all: true
      })
    }
  },

  //选中或者取消选中
  selectItem(e) {
    let index = e.currentTarget.dataset.index
    let list = this.data.list
    if (list[index].select == true) {
      list[index].select = false
    } else {
      list[index].select = true
    }
    this.setData({
      list: list
    })
    this.totalPrice()
    this.isSelectAll()
  },

  add(e) {
    let index = e.currentTarget.dataset.index
    let list = this.data.list
    list[index].num = list[index].num + 1
    this.setData({
      list: list
    })
    this.totalPrice()
  },

  reduce(e) {
    let index = e.currentTarget.dataset.index
    let list = this.data.list

    if (list[index].num > 1) {
      list[index].num = list[index].num - 1
    } else {
      wx.showToast({
        icon: "none",
        title: '数量不能再减了',
        duration: 1500
      })
    }
    this.setData({
      list: list
    })
    this.totalPrice()
  },

  //计算价格
  totalPrice() {
    let list = this.data.list
    let price = 0
    let num = 0
    list.forEach(item => {
      if (item.select == true) {
        price = price + item.price * item.num
        num = num + item.num
      }
    })
    this.setData({
      totalPrice: price.toFixed(2),
      totalNum: num
    })
  },

  //提交续费订单
  pay() {
    let that=this
    if(this.data.canPay==false){
      return
    }
    this.setData({
      canPay:false
    })
    let designPostersId = ""
    let num = ""
    let list = this.data.list
    list.forEach(item => {
      if (item.select == true) {
        designPostersId = item.id
        num = item.num
      }
    })
    if (!designPostersId) {
      wx.showToast({
        icon: "none",
        title: '请选择海报设计服务',
        duration: 1500
      })
      return
    }
    controller.designPostersOrder({
      designPostersId: designPostersId,
      num: num,
      shopId: wx.getStorageSync('shopId')
    }).then(res => {
      let orderNo = res.data.orderNo
      let accountId = res.data.accountId
      let orderId = res.data.id
      controllers.weChatPay({
        shopId: wx.getStorageSync('shopId'),
        orderNo: orderNo,
        frontUrl: "mp:///pages/Index/Index",
        type: "SP",
        openId: wx.getStorageSync('openId')
      }).then(res => {
        console.log(res);
        // wx.navigateTo({
        //   url: '/pages/weixinlink/weixinlink?url='+encodeURIComponent(res.data)
        // })
        let data = JSON.parse(res.data);
        console.log(data)
        wx.requestPayment({
          nonceStr: data.nonceStr,
          package: data.package,
          paySign: data.paySign,
          timeStamp: data.timeStamp,
          signType: data.signType,
          success(res) {
            console.log(res)
          },
          fail(res) {
            console.log(res)
          },
          complete(res){
            that.setData({
              canPay:true
            })
          }
        })
      })

    })
  },




  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      shopId: wx.getStorageSync('shopId'),
      endTime: wx.getStorageSync('endTime').slice(0, 10)
    })
    this.getList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})